<?php

namespace app\cps\service;

use app\common\Error;
use app\model\DistributorsSharingStatistics;
use app\model\DistributionSettlement;
use app\model\ResponsibilityPerson;
use app\model\DistributorsH5SmsSharingStatistics;

class DistributionService
{
    private $distributorsSharingStatisticsModel;
    private $distributionSettlementModel;
    private $responsibilityPersonModel;

    public function __construct(DistributorsSharingStatistics $distributorsSharingStatisticsModel,DistributorsH5SmsSharingStatistics $distributorsH5SmsSharingStatisticsModel, DistributionSettlement $distributionSettlementModel, ResponsibilityPerson $responsibilityPersonModel)
    {
        $this->distributorsSharingStatisticsModel = $distributorsSharingStatisticsModel;
        $this->distributionSettlementModel = $distributionSettlementModel;
        $this->responsibilityPersonModel = $responsibilityPersonModel;
        $this->distributorsH5SmsSharingStatisticsModel = $distributorsH5SmsSharingStatisticsModel;
    }

    // 获取分销商分成统计列表
    public function getDividedStatisticsList($params, $userInfo)
    {
        $where[] = ['distributor_id', '=', $userInfo['cps_user_id']];
        $page = $params['page'] ?? 1;
        $limit = $params['limit'] ?? 10;

        // 日期搜索
        if (!empty($params['start_date'])) {
            $where[] = ['date', '>=', date('Ymd', strtotime($params['start_date']))];
        }
        if (!empty($params['end_date'])) {
            $where[] = ['date', '<=', date('Ymd', strtotime($params['end_date']))];
        }

        $data = $this->distributorsSharingStatisticsModel->getPagingDataByWhere($where, $page, $limit, 'date, name, company_name, recharge_price, refund_price, commission, scale, sharing_amount,virtual_recharge_price,virtual_refund_price,virtual_commission,common_commission,virtual_scale,common_recharge_price,common_refund_price,common_scale,virtual_amount,common_amount');

        if ($data['data']) {
            foreach ($data['data'] as $key => $value) {
                $data['data'][$key]['date'] = substr($value['date'], 0, 4).'-'.substr($value['date'], 4, 2).'-'.substr($value['date'], 6, 2);
            }
        }
        return $data;
    }

    // 分成合计
    public function getDivideIntoTotal($params, $userInfo)
    {
        $where[] = ['distributor_id', '=', $userInfo['cps_user_id']];
        // 日期搜索
        if (!empty($params['start_date'])) {
            $where[] = ['date', '>=', date('Ymd', strtotime($params['start_date']))];
        }
        if (!empty($params['end_date'])) {
            $where[] = ['date', '<=', date('Ymd', strtotime($params['end_date']))];
        }

        $data['recharge_price'] = $this->distributorsSharingStatisticsModel->getCount($where, 'recharge_price');

        $data['commission'] = $this->distributorsSharingStatisticsModel->getCount($where, 'commission');

        $data['refund_price'] = $this->distributorsSharingStatisticsModel->getCount($where, 'refund_price');

        $data['sharing_amount'] = $this->distributorsSharingStatisticsModel->getCount($where, 'sharing_amount');

        $data['virtual_recharge_price'] = $this->distributorsSharingStatisticsModel->getCount($where, 'virtual_recharge_price');

        $data['virtual_refund_price'] = $this->distributorsSharingStatisticsModel->getCount($where, 'virtual_refund_price');

        $data['common_recharge_price'] = $this->distributorsSharingStatisticsModel->getCount($where, 'common_recharge_price');

        $data['common_refund_price'] = $this->distributorsSharingStatisticsModel->getCount($where, 'common_refund_price');

        $data['virtual_amount'] = $this->distributorsSharingStatisticsModel->getCount($where, 'virtual_amount');

        $data['common_amount'] = $this->distributorsSharingStatisticsModel->getCount($where, 'common_amount');

        $data['common_commission'] = $this->distributorsSharingStatisticsModel->getCount($where, 'common_commission');

        $data['virtual_commission'] = $this->distributorsSharingStatisticsModel->getCount($where, 'virtual_commission');

        return $data;
    }

    // 分销结算列表
    public function getDistributionSettlementList($params, $userInfo)
    {
        $where[] = ['distributor_id', '=', $userInfo['cps_user_id']];
        $page = $params['page'] ?? 1;
        $limit = $params['limit'] ?? 10;

        // 状态搜索
        if (!empty($params['status'])) {
            $where[] = ['status', '=', $params['status']];
        }

        $data = $this->distributionSettlementModel->getPagingDataByWhere($where, $page, $limit, 'id, name, start_date, end_date, settlement_price, status, payment_method, tax_rate, payee, collection_account, bank_name, payment_time');
        return $data;
    }

    // 提交结算申请
    public function submitSettlementApply($params)
    {
        try {
            if (empty($params['id'])) {
                return ['message' => Error::REQUEST_PARAMETER_EXCEPTION];
            }

            $info = $this->distributionSettlementModel->getInfoByWhere(['id' => $params['id']], 'status');

            if (empty($info) && $info['status'] != 1) {
                return ['message' => Error::REQUEST_PARAMETER_EXCEPTION];
            }

            $bool = $this->distributionSettlementModel->edit(['id' => $params['id'], 'status' => 2]);

            return true;
        } catch (\Exception $e) {
            return ['message' => $e->getMessage()];
        }
    }

    // 分销商详情
    public function getDistributionDetail($userInfo)
    {
        $cpsId = $userInfo['cps_user_id'];
        $data = $this->responsibilityPersonModel->getInfoById($cpsId, 'is_distributor, pid, is_branch_office_manage');

        if (!empty($data['pid'])) {
            $parentData = $this->responsibilityPersonModel->getInfoById($data['pid'], 'is_distributor, pid, is_branch_office_manage');
            if (!empty($parentData['is_branch_office_manage']) && $parentData['is_branch_office_manage'] == 1) {
                $data['is_branch_office_pitcher'] = 1;
            } else {
                $data['is_branch_office_pitcher'] = 0;
            }
        }
        return $data;
    }

    // 分销商H5短信分成列表
    public function getSmsSharingList($params, $userInfo)
    {
        $page = $params['page'] ?? 1;
        $limit = $params['limit'] ?? 10;

        $where = [];
        // 日期搜索
        if (!empty($params['start_date'])) {
            $where[] = ['date', '>=', date('Ymd', strtotime($params['start_date']))];
        }
        if (!empty($params['end_date'])) {
            $where[] = ['date', '<=', date('Ymd', strtotime($params['end_date']))];
        }
        $where[] = ['distributor_id', '=', $userInfo['cps_user_id']];

        $data = $this->distributorsH5SmsSharingStatisticsModel->getPagingDataByWhere($where, $page, $limit, 'date,name,recharge_price,refund_price,commission,sharing_amount,ly_sharing_amount');

        if ($data['data']) {
            foreach ($data['data'] as $key => $value) {
                $data['data'][$key]['date'] = substr($value['date'], 0, 4).'-'.substr($value['date'], 4, 2).'-'.substr($value['date'], 6, 2);
            }
        }

        return $data;
    }
}